/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.samlss.lighter_demo.dialog;

import me.samlss.lighter.Lighter;
import me.samlss.lighter.parameter.Direction;
import me.samlss.lighter.parameter.LighterParameter;
import me.samlss.lighter.parameter.MarginOffset;
import me.samlss.lighter.shape.RectShape;
import me.samlss.lighter.util.PixelUtil;
import me.samlss.lighter_demo.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TableLayout;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

import java.util.Optional;
/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class CustomDialog extends CommonDialog {
    private Component mComponent;
    private Context mContext;
    private Ability mAbility;

    /**
     * getOwnerAbility
     * @return Ability
     */
    public Ability getOwnerAbility() {
        return mAbility;
    }

    /**
     *
     * @param context
     */
    public CustomDialog(Context context) {
        super(context);
        mContext = context;
        if (context instanceof Ability) {
            mAbility = (Ability) context;
        }
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        mComponent = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_dialog, null, false);
        setContentCustomComponent(mComponent);
    }

    @Override
    public void show() {
        super.show();
        Optional<WindowManager.LayoutConfig> layoutConfigOptional = getWindow().getLayoutConfig();
        if (layoutConfigOptional.isPresent()) {
            WindowManager.LayoutConfig layoutConfig = layoutConfigOptional.get();
            layoutConfig.width = (int) (PixelUtil.screenWidth() * 0.9f);
            layoutConfig.height = (int) (PixelUtil.screenHeight() * 0.8f);
            layoutConfig.alignment = LayoutAlignment.CENTER;
            getWindow().setLayoutConfig(layoutConfig);
        }
        showGuide();
    }

    private void showGuide() {
        Lighter.with((ComponentContainer) mComponent)
                .setBackgroundColor(0x000000, 0.5f)
                .addHighlight(
                        //Show two at a time
                        new LighterParameter.Builder()
                                .setHighlightedViewId(ResourceTable.Id_vp_btn_1)
                                .setTipLayoutId(ResourceTable.Layout_layout_tip_1)
                                .setLighterShape(new RectShape(5, 5, 30))
                                .setTipViewRelativeDirection(Direction.BOTTOM)
                                .setTipViewRelativeOffset(new MarginOffset(150, 0, 30, 0))
                                .build(),

                        new LighterParameter.Builder()
                                .setHighlightedViewId(ResourceTable.Id_vp_btn_2)
                                .setTipLayoutId(ResourceTable.Layout_layout_tip_2)
                                .setLighterShape(new RectShape(5, 5, 30))
                                .setTipViewRelativeDirection(Direction.TOP)
                                .setTipViewRelativeOffset(new MarginOffset(-400, 0, 0, 30))
                                .build())
                .show();
    }
}
